BEGIN {Bins=3 ; OFS=FS=","}
NR==1{ Header=$0;
       for(I=1;I<=NF;I++) readHeader(I,$I,Name,Index,Num,Sym,Dep,Indep)
       next
     }
     { N++
       for(I in Name) Data[N,I]=$I  
     }
function asBins() {
    #o(Num,"Num"); exit
    for(col in Num) {
	fyi("discretizing " col)
	discretize(col,N,Data,Bins,1,breaks)
	for(row=1;row<=N;row++) 
	    Data[row,col] = asBin(col,Data[row,col],Bins,breaks)
	}
    gsub(/\$/,"",Header)
    print Header
    for(row=1;row<=N;row++) {
	out=Data[row,1]
	for(col=2;col<=NF;col++) out=out OFS Data[row,col]
	print out
}}

function histograms(            i,b1,c1,b2,c2) {
    for (i=1;i<=NF;i++) if (i in Num) {
	print "\n=============================\n" Name[i]
	print i ": " Name[i] >> "/dev/stderr"
	discretize(i,N,Data,Bins,1,b1,c1,1,b2,c2) 
	print("\nequal width:");     histogram(N,i,b2,c2,4)
	print("\nequal frequency:"); histogram(N,i,b1,c1,4)           
    }}

function histogram(n,col,b,c,  shrink,com,   i,tmp,what,p) {
    shrink = shrink ? shrink : 1
    com = (com ? "sort " com  : "sort -n -k 3 ") " #" rand()
    printf("bin | %10s | %5s (%)\n","break","count") 
    printf("--- | ---------- | ----- -------\n")
    for(i in b) {
	split(i,tmp,_) 
	#print col " " 1 " " tmp[1] " " 2 " " tmp[2]
        if (tmp[1] == col) {
	    p = 100*c[i]/n
	    printf("%3s | %10.2f | %5.0f (%6.2f) %s\n", tmp[2], b[i], c[i], p, nchars(p/shrink,"*")) | com
	}}
    close(com)
}

	
